package com.food.restaurantpro.database.tables;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.util.Log;

import com.food.restaurantpro.RestaurantApp;
import com.food.restaurantpro.database.RestaurantAppContentProvider;
import com.food.restaurantpro.objects.Food;

public class FoodTable {
	public static final String TAG = "FoodTable";
	
	public static final String TABLE_NAME = "food";
	
	public static final String FOOD_ID = "foodID";
	public static final String FOOD_NAME = "foodName";
	public static final String FOOD_DESCRIPTION = "foodDescription";
	public static final String FOOD_IMAGE = "foodImage";
	public static final String FOOD_PRICE = "foodPrice";
	public static final String FOOD_TYPEID = "foodTypeID";
	public static final String FOOD_STATUS = "foodStatus";

	public static final String SQL_EXCUTE = "CREATE TABLE " + TABLE_NAME + "("
			+ FOOD_ID + " text primary key not null, "
			+ FOOD_NAME + " text not null, "
			+ FOOD_DESCRIPTION + " text not null, "
			+ FOOD_IMAGE + " text not null, "
			+ FOOD_PRICE + " text not null, "
			+ FOOD_TYPEID + " text not null, "
			+ FOOD_STATUS + " text not null);";
	
	public static void deleteAll()
	{
		ContentResolver resolver = RestaurantApp.CURRENT_CONTEXT.getContentResolver();
		resolver.delete(RestaurantAppContentProvider.FOOD_CONTENT_URI, null, null);
	}
	/**
	 * @param listFOODs list FOODs to insert to db
	 * @return the number of newly created rows. 
	 */
	public static int insertListFood(List<Food> foodList) {
		if(foodList != null && foodList.size() > 0) {
			int size = foodList.size();
			//show progress
			ContentValues[] allValues = new ContentValues[size];
			for (int i = 0; i < size; i++) {
				ContentValues value = new ContentValues();
				Food food = foodList.get(i);
				value.put(FOOD_ID, food.getFoodID());
				value.put(FOOD_NAME, food.getFoodName());
				value.put(FOOD_DESCRIPTION, food.getFoodDescription());
				value.put(FOOD_IMAGE, food.getFoodImage());
				value.put(FOOD_PRICE, food.getFoodPrice());
				value.put(FOOD_TYPEID, food.getFoodTypeID());
				value.put(FOOD_STATUS, food.getFoodStatus());
				
				allValues[i] = value;
			}
			ContentResolver resolver = RestaurantApp.CURRENT_CONTEXT.getContentResolver();
			int rowadd = resolver.bulkInsert(RestaurantAppContentProvider.FOOD_CONTENT_URI, allValues);
			return rowadd;
			//clode progress
		}
		return 0;
	}
	
	public static ArrayList<String> getAllFoodId() {

		ContentResolver resolver = RestaurantApp.CURRENT_CONTEXT.getContentResolver();
		ArrayList<String> arrayList = new ArrayList<String>();
		String[] columns = new String[]{FOOD_ID};
		Cursor cursor = resolver.query(RestaurantAppContentProvider.FOOD_CONTENT_URI, columns, null, null, FOOD_NAME + " ASC");
		if(cursor != null){
			if(cursor.getCount()>0){
				try {
					while (!cursor.isLast()) {
						cursor.moveToNext();
						String foodId = cursor.getString(cursor.getColumnIndex(FOOD_ID));
						arrayList.add(foodId);
					}
				} catch (Exception e) {
					// TODO: handle exception
					Log.e(TAG, "ERROR getAllFoodId()", e);
					arrayList = null;
				}
			}
			cursor.close();
		}
		return arrayList;
	}
	
	public static ArrayList<Food> getAllFoods()
	{
		ContentResolver resolver = RestaurantApp.CURRENT_CONTEXT.getContentResolver();
		ArrayList<Food> arrayList = new ArrayList<Food>();
		Cursor cursor = resolver.query(RestaurantAppContentProvider.FOOD_CONTENT_URI, null, null, null, FOOD_NAME + " ASC");
		if(cursor != null){
			//cach 1
			/*if(cursor.moveToFirst()) {
				do{
					//TODO
				} while(cursor.moveToNext());
			}*/
			//cach 2
			if(cursor.getCount()>0){
				try {
					while (!cursor.isLast()) {
						cursor.moveToNext();
						Food food = new Food();
						
						String foodID = cursor.getString(cursor.getColumnIndex(FOOD_ID));
						food.setFoodID(foodID);
						
						String foodName = cursor.getString(cursor.getColumnIndex(FOOD_NAME));
						food.setFoodName(foodName);
						
						String foodDescription = cursor.getString(cursor.getColumnIndex(FOOD_DESCRIPTION));
						food.setFoodDescription(foodDescription);
						
						String foodImage = cursor.getString(cursor.getColumnIndex(FOOD_IMAGE));
						food.setFoodImage(foodImage);
						
						String foodPrice = cursor.getString(cursor.getColumnIndex(FOOD_PRICE));
						food.setFoodPrice(foodPrice);
						
						String foodTypeID = cursor.getString(cursor.getColumnIndex(FOOD_TYPEID));
						food.setFoodTypeID(foodTypeID);
						
						String foodStatus = cursor.getString(cursor.getColumnIndex(FOOD_STATUS));
						food.setFoodStatus(foodStatus);
						
						arrayList.add(food);
					}
				} catch (Exception e) {
					// TODO: handle exception
					Log.e(TAG, "ERROR getAllFoods()", e);
				}
			}
			cursor.close();
		}
		return arrayList;
	}
	
	public static ArrayList<Food> getFoodListByFoodTypeID(String dishCategoryId)
	{
		ContentResolver resolver = RestaurantApp.CURRENT_CONTEXT.getContentResolver();
		ArrayList<Food> arrayList = new ArrayList<Food>();
		Cursor cursor = resolver.query(RestaurantAppContentProvider.FOOD_CONTENT_URI, null, FOOD_TYPEID + "=?", new String[] {dishCategoryId}, FOOD_NAME + " ASC");
		if(cursor != null){
			//cach 1
			/*if(cursor.moveToFirst()) {
				do{
					//TODO
				} while(cursor.moveToNext());
			}*/
			//cach 2
			if(cursor.getCount()>0){
				try {
					while (!cursor.isLast()) {
						cursor.moveToNext();
						Food food = new Food();
						
						String foodID = cursor.getString(cursor.getColumnIndex(FOOD_ID));
						food.setFoodID(foodID);
						
						String foodName = cursor.getString(cursor.getColumnIndex(FOOD_NAME));
						food.setFoodName(foodName);
						
						String foodDescription = cursor.getString(cursor.getColumnIndex(FOOD_DESCRIPTION));
						food.setFoodDescription(foodDescription);
						
						String foodImage = cursor.getString(cursor.getColumnIndex(FOOD_IMAGE));
						food.setFoodImage(foodImage);
						
						String foodPrice = cursor.getString(cursor.getColumnIndex(FOOD_PRICE));
						food.setFoodPrice(foodPrice);
						
						String foodTypeID = cursor.getString(cursor.getColumnIndex(FOOD_TYPEID));
						food.setFoodTypeID(foodTypeID);
						
						String foodStatus = cursor.getString(cursor.getColumnIndex(FOOD_STATUS));
						food.setFoodStatus(foodStatus);
						
						arrayList.add(food);
					}
				} catch (Exception e) {
					// TODO: handle exception
					Log.e(TAG, "ERROR getAllFoods()", e);
				}
			}
			cursor.close();
		}
		return arrayList;
	}
	
	public static void insertFood(Food food) {
		if(food != null) {
			ContentValues value = new ContentValues();
			value.put(FOOD_ID, food.getFoodID());
			value.put(FOOD_NAME, food.getFoodName());
			value.put(FOOD_DESCRIPTION, food.getFoodDescription());
			value.put(FOOD_IMAGE, food.getFoodImage());
			value.put(FOOD_PRICE, food.getFoodPrice());
			value.put(FOOD_TYPEID, food.getFoodTypeID());
			value.put(FOOD_STATUS, food.getFoodStatus());
				
			ContentResolver resolver = RestaurantApp.CURRENT_CONTEXT.getContentResolver();
			resolver.insert(RestaurantAppContentProvider.FOOD_CONTENT_URI, value);
		}
	}
	
	public static int updateFoodById(Food food) {
		if(food != null) {
			ContentValues value = new ContentValues();
			value.put(FOOD_ID, food.getFoodID());
			value.put(FOOD_NAME, food.getFoodName());
			value.put(FOOD_DESCRIPTION, food.getFoodDescription());
			value.put(FOOD_IMAGE, food.getFoodImage());
			value.put(FOOD_PRICE, food.getFoodPrice());
			value.put(FOOD_TYPEID, food.getFoodTypeID());
			value.put(FOOD_STATUS, food.getFoodStatus());
				
			ContentResolver resolver = RestaurantApp.CURRENT_CONTEXT.getContentResolver();
			int rowUpdate = resolver.update(RestaurantAppContentProvider.FOOD_CONTENT_URI, value, FOOD_ID + "=?", new String[] {food.getFoodID()});
			return rowUpdate;
		}
		return -1;
	}
	
	public static void deleteFoodById(String foodId)
	{
		ContentResolver resolver = RestaurantApp.CURRENT_CONTEXT.getContentResolver();
		resolver.delete(RestaurantAppContentProvider.FOOD_CONTENT_URI, FOOD_ID + "=?", new String[] {foodId});
	}
}
